package com.ruoyi.bookSys.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.bookSys.domain.BookBorrow;
import com.ruoyi.bookSys.mapper.BookReaderMapper;
import com.ruoyi.bookSys.domain.BookReader;
import com.ruoyi.bookSys.service.IBookReaderService;

/**
 * 读者信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-14
 */
@Service
public class BookReaderServiceImpl implements IBookReaderService 
{
    @Autowired
    private BookReaderMapper bookReaderMapper;

    /**
     * 查询读者信息
     * 
     * @param readerId 读者信息主键
     * @return 读者信息
     */
    @Override
    public BookReader selectBookReaderByReaderId(Long readerId)
    {
        return bookReaderMapper.selectBookReaderByReaderId(readerId);
    }

    /**
     * 查询读者信息列表
     * 
     * @param bookReader 读者信息
     * @return 读者信息
     */
    @Override
    public List<BookReader> selectBookReaderList(BookReader bookReader)
    {
        return bookReaderMapper.selectBookReaderList(bookReader);
    }

    /**
     * 新增读者信息
     * 
     * @param bookReader 读者信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBookReader(BookReader bookReader)
    {
        bookReader.setCreateTime(DateUtils.getNowDate());
        int rows = bookReaderMapper.insertBookReader(bookReader);
        insertBookBorrow(bookReader);
        return rows;
    }

    /**
     * 修改读者信息
     * 
     * @param bookReader 读者信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBookReader(BookReader bookReader)
    {
        bookReader.setUpdateTime(DateUtils.getNowDate());
        bookReaderMapper.deleteBookBorrowByReaderId(bookReader.getReaderId());
        insertBookBorrow(bookReader);
        return bookReaderMapper.updateBookReader(bookReader);
    }

    /**
     * 批量删除读者信息
     * 
     * @param readerIds 需要删除的读者信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookReaderByReaderIds(Long[] readerIds)
    {
        bookReaderMapper.deleteBookBorrowByReaderIds(readerIds);
        return bookReaderMapper.deleteBookReaderByReaderIds(readerIds);
    }

    /**
     * 删除读者信息信息
     * 
     * @param readerId 读者信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookReaderByReaderId(Long readerId)
    {
        bookReaderMapper.deleteBookBorrowByReaderId(readerId);
        return bookReaderMapper.deleteBookReaderByReaderId(readerId);
    }

    /**
     * 新增图书借阅记录信息
     * 
     * @param bookReader 读者信息对象
     */
    public void insertBookBorrow(BookReader bookReader)
    {
        List<BookBorrow> bookBorrowList = bookReader.getBookBorrowList();
        Long readerId = bookReader.getReaderId();
        if (StringUtils.isNotNull(bookBorrowList))
        {
            List<BookBorrow> list = new ArrayList<BookBorrow>();
            for (BookBorrow bookBorrow : bookBorrowList)
            {
                bookBorrow.setReaderId(readerId);
                list.add(bookBorrow);
            }
            if (list.size() > 0)
            {
                bookReaderMapper.batchBookBorrow(list);
            }
        }
    }
}
